package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.JurisdictionService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JurisdictionServiceImpl implements JurisdictionService {

    @Autowired
    ISysDeptService deptService;

    @Autowired
    ISysUserService userService;

    @Override
    public Map getPersonInfo() {
        Map resMap = new HashMap();
        // step1:先找用户信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String deptName = loginUser.getUser().getDept().getDeptName();
        Long userId = loginUser.getUserId();
        // step2:找业务及运营人员列表
        Long deptId = loginUser.getDeptId();
        // 查看部门层级
        int deptLevel = deptService.getDeptLevel(deptId);
        Long parentId = 0L;
        Long ywryDepeId;
        Long yyryDepeId;

        if (deptLevel == 1) {
            parentId = deptId;
        } else if (deptLevel == 2) {
            parentId = deptService.selectDeptById(loginUser.getDeptId()).getParentId();
        }

        SysDept parentDept = deptService.selectDeptById(parentId);

        // 如果是1级部门,代表是某公司的主管理员账号,找该公司业务组下面的所有人
        SysDept dept = new SysDept();
        dept.setParentId(parentId);
        dept.setDeptName("业务组");
        List<SysDept> ywzDepts = deptService.selectDeptList(dept);
        ywryDepeId = ywzDepts.get(0).getDeptId();
        dept.setDeptName("运营组");
        List<SysDept> yyzDepts = deptService.selectDeptList(dept);
        yyryDepeId = yyzDepts.get(0).getDeptId();

        List<SysUser> yyryUsers;
        List<SysUser> ywryUsers;

        if ("运营组".equals(deptName)) {
            SysUser user = new SysUser();
            user.setDeptId(yyryDepeId);
            user.setUserId(userId);
            yyryUsers = userService.selectUserList(user);
            user.setDeptId(ywryDepeId);
            user.setUserId(null);
            ywryUsers = userService.selectUserList(user);
        } else if ("业务组".equals(deptName)) {
            SysUser user = new SysUser();
            user.setDeptId(ywryDepeId);
            user.setUserId(userId);
            ywryUsers = userService.selectUserList(user);
            user.setDeptId(yyryDepeId);
            user.setUserId(null);
            yyryUsers = userService.selectUserList(user);
        } else {
            SysUser user = new SysUser();
            user.setDeptId(ywryDepeId);
            ywryUsers = userService.selectUserList(user);
            user.setDeptId(yyryDepeId);
            yyryUsers = userService.selectUserList(user);
        }
        resMap.put("userInfo", loginUser);
        resMap.put("ywryUsers", ywryUsers);
        resMap.put("yyryUsers", yyryUsers);
        resMap.put("ssgs", parentDept);
        return resMap;
    }

    @Override
    public String getHotelDataPermissionsAndCompleteSQL () {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        // 部门信息
        SysDept sysDept = loginUser.getUser().getDept();
        String deptName = sysDept.getDeptName();
        Long deptId = sysDept.getDeptId();

        // 运营或业务以人员纬度作为权限
        if ("运营组".equals(deptName)) {
            return " and yyry = " + userId + " ";
        } else if ("业务组".equals(deptName)) {
            return " and ywry = " + userId + " ";
        } else {
            // 管理员或客服以公司纬度作为权限
            Long parentId = 0L;
            String ancestors = sysDept.getAncestors();
            int deptLevel = StringUtils.countKeywordOccurrences(ancestors, ",");
            if (deptLevel == 1) {
                parentId = deptId;
            } else if (deptLevel == 2) {
                parentId = deptService.selectDeptById(loginUser.getDeptId()).getParentId();
            }
            // 超级管理员给悦游总部的权限
            if(parentId == 0){
                parentId = 111L;
            }
            return " and ssgs = " + parentId + " ";
        }
    }
}